<template>
  <div class="app-container" v-loading.fullscreen.lock="fullLoading">

    <el-table v-loading="loading" :data="iamList">
      <el-table-column label="提交人" align="center" prop="submitter" />
      <el-table-column label="学年" align="center" prop="stuYearName" width="240" />
      <el-table-column label="加分人" align="center" prop="stuName" />
      <el-table-column label="分数" align="center" prop="iamScore" />
      <el-table-column label="加分项" align="center" prop="ruleName" />
      <el-table-column label="备注" align="center" prop="description" />
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audius_detail_type" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-folder" @click="detailVClick(scope.row)">详情</el-button>
          <el-button v-if="scope.row.auditStatus == '6'" size="mini" type="text" icon="el-icon-circle-close"
            @click="cancelVClick(scope.row)">取消加/扣分</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <el-dialog :visible.sync="detailV" title="分数详情">
      <el-form label-width="100px">
        <el-form-item label="学院" prop="projectName">
          <el-input v-model="detailInfo.deptName" readonly />
        </el-form-item>
        <el-form-item label="专业" prop="projectName">
          <el-input v-model="detailInfo.majorName" readonly />
        </el-form-item>
        <el-form-item label="班级" prop="projectName">
          <el-input v-model="detailInfo.className" readonly />
        </el-form-item>
        <el-form-item label="分数" prop="projectName">
          <el-input v-model="detailInfo.iamScore" readonly />
        </el-form-item>
        <el-form-item label="加分学生" prop="submitterName">
          <el-input v-model="detailInfo.stuName" readonly />
        </el-form-item>
        <el-form-item label="加分项目" prop="submitterName">
          <el-input v-model="detailInfo.ruleName" readonly />
        </el-form-item>
        <el-form-item label="描述" prop="submitterName">
          <el-input v-model="detailInfo.description" type="textarea" readonly />
        </el-form-item>
      </el-form>
      <el-carousel indicator-position="outside">
        <el-carousel-item v-for="(v, i) in material_list" :key="i" style="text-align: center;">
          <el-image :src="getImg(v)" style="height: 100%;"></el-image>
        </el-carousel-item>
      </el-carousel>
      <div slot="footer" class="dialog-footer">
      </div>
    </el-dialog>

    <el-dialog title="取消加/扣分" :visible.sync="cancelV" append-to-body>
      <el-form label-width="100px">
        <el-form-item label="理由">
          <el-input v-model="cancelDto.reason" type="textarea" placeholder="您为什么要取消呢?请说明原因(例如:辅导员点错了)"/>
        </el-form-item>
        <el-form-item label="">
          <el-button @click="cancelProcess">确定提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>
  
<script>
import { getStuByClass, getIam, delIam, addIam, updateIam, listLeftJoin, fdyAdd, listIamByStuNoYear } from "@/api/stuCQS/info-fill/iam";
import { treeStudent } from "@/api/stuCQS/basedata/student";
import { listYear } from "@/api/stuCQS/basedata/year";
import { listIamChild } from "@/api/stuCQS/basedata/cphRules";
import store from "@/store";
import { listOwnClass, listOwnStuByClass } from "@/api/stuCQS/info-fill/stu_eva_task";
import { isEmpty } from "@/api/helpFunc";
import lodash from "lodash";
import { cancelProcess } from "@/api/stuCQS/process-center/auditDetails";


export default {
  name: "IamDetailList",
  dicts: ["audius_detail_type"],
  data() {
    return {
      fullLoading:false,

      cancelV: false,
      cancelDto: {
        id: null,
        reason: ""
      },

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 思想品德表格数据
      iamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        submitter: null,
        toAdd: null,
        material: null,
        description: null,
        auditStatus: null,
        stuYearId: null,
        params: {}
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        auditStatus: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
        delFlag: [
          { required: true, message: "删除标记不能为空", trigger: "blur" }
        ],
      },

      //学生列表
      stu_list: [],

      //班级列表
      class_list: [],
      //学年列表
      stu_year_list: [],
      //选择的学生
      toAdd: null,
      //选择的加分项
      childType: null,
      //加分项列表
      child_list: [],
      //加分分值
      add_record: 0,
      //详情页
      detailV: false,
      //附件
      material_list: [],

      cascaderData: null,

      fdyV: false,
      isFdy: false,
      own_class: [],
      own_stu: [],
      selectedClass: null,
      selectedStu: null,
      fdyForm: {},
      detailInfo: {
        student: {},
        rules: {}
      }
    };
  },
  watch: {

    "childType"(newVal, oldVal) {
      this.child_list.map((v) => {
        if (v.ruleId == newVal) {
          this.add_record = v.maxScore
        }
      });
    }
  },
  created() {


    this.getList();
    //this.getStuTree();
    //this.getStuYear();
    //this.listIamChild();
    //this.getCascaderData();
    let temp = store.getters.roles;
    temp.map(x => {
      if (x == "testteacher") {
        this.isFdy = true;
        this.listOwnClass();
      }

    });

  },
  methods: {
    cancelVClick(v) {
      console.log(v);
      this.cancelDto.id = parseInt(v.iamId);
      this.cancelV = true;
    },
    async cancelProcess() {
      let sdata = lodash.cloneDeep(this.cancelDto);
      if (isEmpty(sdata.reason)) {
        this.$message.info("请说明理由");
        return;
      }
      this.fullLoading = true;
      let res = await cancelProcess(sdata);
      this.fullLoading = false;
      if (res.code == 200) {
        this.$message.success(res.msg);
        this.getList();
      }
      this.cancelV = false;
    },

    async listOwnClass() {
      let res = await listOwnClass();
      if (res.code == 200) {
        this.own_class = [...res.data];
      }
    },
    async getClassStu() {
      let class_id = this.selectedClass;
      if (class_id != null) {
        let res = await listOwnStuByClass(class_id);
        this.own_stu = [...res.data];
      }
    },
    async fdyAdd() {
      let sdata = { ...this.fdyForm };
      if (this.selectedStu == null) {
        this.$message.info("请选择学生");
        return;
      }
      if (this.childType == null) {
        this.$message.info("请选择项目");
        return;
      }
      if (isEmpty(sdata.stuYearId)) {
        this.$message.info("请选择学年");
        return;
      }

      sdata.stuNo = this.selectedStu;
      sdata.ruleId = this.childType;
      let res = await fdyAdd(sdata);
      if (res.code == 200) {
        this.$message.success(res.msg);
        this.getList();
      }
    },

    // 级联选择数据
    async getCascaderData() {
      const res = await treeStudent()
      this.cascaderData = res.data
      this.cascaderData.forEach(dept => {
        dept.children.forEach(major => {
          major.children.forEach(c => {
            delete c.children
          })
        })
      });
    },
    // 级联选择的项
    handleChange(value) {
      this.queryParams = {
        ...this.queryParams,
        classId: value[2],
      }
    },
    async changeClass(val) {
      let class_id = val[val.length - 1];
      let res = await getStuByClass(class_id);
      this.toAdd = null;
      this.stu_list = res.data;
    },
    getImg(src) {
      return process.env.VUE_APP_BASE_API + src
    },

    //点击详情
    detailVClick(val) {
      this.detailInfo = { ...val };
      console.log(this.detailInfo);
      if (!isEmpty(this.detailInfo.material)) {
        this.material_list = this.detailInfo.material.split(",");
      }

      this.detailV = true;
    },

    //获取审核状态
    getProcessState(state) {
      let res = "";
      let dicts = this.dict.type.audius_detail_type;
      dicts.map(x => {
        if (x.value == state) {
          res = x.label;
        }
      });
      return res;
    },

    async listIamChild() {
      let res = await listIamChild();
      this.child_list = [...res.data];
    },

    //树形学生信息
    async getStuTree() {
      let res = await treeStudent();
      let data = res.data;
      this.class_list = [...data];
    },

    //列出学年信息
    async getStuYear() {
      let res = await listYear();
      this.stu_year_list = [...res.rows];
    },

    /** 查询思想品德列表 */
    getList() {
      this.loading = true;
      var stuscore = this.$store.iamStuScore.stuNo;
      var stuYearId = this.$store.iamStuScore.stuYearId;
      this.queryParams.stuNo = stuscore;
      this.queryParams.stuYearId = stuYearId;
      console.log(this.queryParams);
      listIamByStuNoYear(this.queryParams).then(response => {
        console.log(response);
        this.iamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        submitter: null,
        toAdd: null,
        material: null,
        description: null,
        auditStatus: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        stuYearId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加思想品德";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getIam(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改思想品德";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateIam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            let sdata = {
              stuYearId: this.form.stuYearId,
              toAdd: this.toAdd,
              material: this.form.material,
              description: this.form.description,
              ruleId: this.childType,
            }
            addIam(sdata).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除思想品德编号为"' + ids + '"的数据项？').then(function () {
        return delIam(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/iam/export', {
        ...this.queryParams
      }, `iam_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.el-carousel__item h3 {
  color: #475669;
  font-size: 18px;
  opacity: 0.75;
  line-height: 300px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}
</style>